package net.siufung.market.base.permit.controller.regulation;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.AllArgsConstructor;
import net.siufung.core.exception.BizServiceException;
import net.siufung.core.model.regulation.AbstractRegulation;
import net.siufung.market.base.permit.mapper.IMenuMapper;
import net.siufung.market.base.permit.mapper.entity.Menu;

import java.util.Objects;

/**
 * @author 陈建峰
 * @since 2022/5/16 2:54 下午
 */
@AllArgsConstructor
public class MenuDeleteRegulation extends AbstractRegulation<Integer> {

    private static final long serialVersionUID = -5051748809916243494L;

    private final IMenuMapper menuMapper;

    @Override
    public boolean isSatisfiedBy(Integer menuId) {
        if(Objects.isNull(menuId)){
            throw new BizServiceException("菜单主键不能为空！");
        }
        long childCount = menuMapper.selectCount(
                new LambdaQueryWrapper<Menu>().eq(Menu::getParentId, menuId));
        if(childCount > 0){
            throw new BizServiceException("存在【"+childCount+"】个子菜单，不能删除！");
        }
        return Boolean.TRUE;
    }
}
